package com.woniuxy.lrz.carlproject.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.util.Results;
import com.woniuxy.lrz.carlproject.controller.form.formcar.CarAddOrUpdateForm;
import com.woniuxy.lrz.carlproject.controller.form.formcar.CarTypeAddOrUpdateForm;
import com.woniuxy.lrz.carlproject.controller.form.shopform.formcar.CarTypeFindForm;
import com.woniuxy.lrz.carlproject.model.CarType;
import com.woniuxy.lrz.carlproject.service.CarTypeService;
import com.woniuxy.lrz.carlproject.service.dto.dtocar.CarTypeBrandDto;
import com.woniuxy.lrz.carlproject.service.dto.dtocar.CarTypeDto;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 汽车车型表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@Slf4j
@RequestMapping("/carlproject/carType")
@Validated
public class CarTypeController {

    @Resource
    private CarTypeService carTypeService;

    @PostMapping("/addOrUpdateCarType")
    @ApiOperation("新增或修改车型信息")
    public Results addOrUpdateCarType(@RequestBody @Valid CarTypeAddOrUpdateForm carTypeAddOrUpdateForm){
        carTypeService.addOrUpdateCarType(carTypeAddOrUpdateForm);
        return Results.instance().setMessage("操作成功");
    }

    @DeleteMapping("/deleteCarType")
    @ApiOperation("删除车辆型号信息")
    @ApiImplicitParam(name = "carTypeId",value = "车型Id")
    public Results deleteCarType(@NotNull(message = "车型Id不能为空") Integer carTypeId){
        boolean b = carTypeService.deleteCarType(carTypeId);
        return Results.instance().setMessage("删除成功");
    }

    @GetMapping("/findCarType")
    @ApiOperation("加载车型列表")
    public Results findCarType(@Valid CarTypeFindForm carTypeFindForm){
        Page<CarTypeBrandDto> carType = carTypeService.findCarType(carTypeFindForm);
        return Results.instance().setMessage("加载车型列表成功").setData(carType);
    }


    @Cacheable(value = "car",key = "'findType:'+#carBrandId")
    @ApiOperation("加载车型下拉列表并存到redis中")
    @ApiImplicitParam(name = "carBrandId",value = "品牌Id")
    @GetMapping("/findCarTypeCondition")
    public Results findCarTypeCondition(@NotNull(message = "品牌Id不能为空") Integer carBrandId){
        List<CarType> car_brand_id = carTypeService.list(new QueryWrapper<CarType>().eq("car_brand_id", carBrandId));
        List<CarTypeDto> carTypeDtos = BeanUtil.copyToList(car_brand_id, CarTypeDto.class);
        return Results.instance().setMessage("加载车型下拉列表成功").setData(carTypeDtos);
    }








}
